home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Compute`s Amiga resource 1.adf / Source / AskRobPeck / gad.c
C/C++ Source or Header  |  1989-02-07  |  6KB  |  222 lines

  1. /* gad.c */
  2.  
  3. #include "exec/types.h"
  4. #include "intuition/intuition.h"
  5. #include "exec/memory.h"
  6.  
  7. char buffer;
  8. ULONG wakeupbit, cx, cy, maxw, maxh;
  9. USHORT cl, co, qu, mo, mx, my;
  10. APTR ad; /* address of the gadget which we hit */
  11.  
  12. char *phrases[] = { "item1","item2","item3","item4",
  13.  "item5","item6","item7","item8","item9","my10","my11",
  14.  "my12","my13","my14","my15","my16","my17","my18","my19",
  15.  "my20","my21","my22","my23","my24","my25","my26","my27",
  16.  "my28","my29",0,0,0,0,0,0,0,0,0,0,0 }; /* no text last 10 */
  17.  
  18. struct Window *w, *OpenWindow();
  19. struct RastPort *rp;
  20. struct IntuiMessage *mg;
  21. struct Message *GetMsg();
  22. struct Library *IntuitionBase, *GfxBase, *OpenLibrary();
  23.  
  24. #define SAVEMSG {cl=mg->Class;co=mg->Code;qu=mg->Qualifier;\
  25.  ad=mg->IAddress;mx=mg->MouseX;my=mg->MouseY;\
  26.  ReplyMsg(mg);}
  27.  
  28. #define MAXCHARS 5 /* how many chars in each box */
  29. #define TOP 52 /* where to begin from GZZwindow top */
  30. #define LEFT 16 /* where to begin from GZZwin left */
  31. #define CWIDTH 8 /* width of character (using TOPAZ 8) */
  32. #define WWIDTH (MAXCHARS * CWIDTH) /* width of word, 5 char max */
  33. #define HSPACE ((CWIDTH * 2) + WWIDTH) /* horizontal space/gad */
  34. #define HEIGHT 8 /* height of TOPAZ 8 is 8 lines */
  35. #define VSPACE (HEIGHT * 2 - 1) /* vertical space between boxes */
  36. #define MIN(a,b) (a < b ? a : b)
  37.  
  38. struct Gadget abbrev_select = { /* starts relative to GZZ window */
  39.  NULL, 12,47, 568,68, GADGHNONE, RELVERIFY+GADGIMMEDIATE,
  40.  BOOLGADGET,NULL,NULL,NULL,0,NULL,99, NULL };
  41. struct NewWindow nw = {
  42.  18,12,597,147,0,1,GADGETDOWN+CLOSEWINDOW, 
  43.  WINDOWDRAG+WINDOWCLOSE+SMART_REFRESH+GIMMEZEROZERO, 
  44.  &abbrev_select,NULL,(UBYTE *) "Multi-Gadget Test",
  45.  NULL,NULL,100,50,640,200,WBENCHSCREEN };
  46.  
  47. /* convert win-rel mouse-clicks to main gadget internal x,y */
  48. /* NOTE: based on a GZZ window!! */
  49. #define GY -17+my-abbrev_select.TopEdge
  50. #define GX -9+mx-abbrev_select.LeftEdge
  51.  
  52. char *itoa(x)
  53. int x;
  54. {
  55.  buffer = (char)(0x30 + x);
  56.  return(&buffer);
  57. }
  58.  
  59. void DrawBox(x, y) /* draw it downward and to the right */
  60. short x,y;
  61. {
  62.  SetDrMd(rp,JAM2);
  63.  SetAPen(rp,3);
  64.  Move(rp,x-1,y-1);
  65.  Draw(rp,x+WWIDTH+1, y-1);
  66.  Draw(rp,x+WWIDTH+1, y+HEIGHT+1);
  67.  Draw(rp,x-1, y+HEIGHT+1);
  68.  Draw(rp,x-1, y);
  69. }
  70.  
  71. void ShowCursor(x,y) /* Same as drawbox but fill it */
  72. long x,y;   
  73. {
  74.  SetDrMd(rp, COMPLEMENT); 
  75.  /* De-highlite old "gadget". */
  76.  RectFill(rp, cx, cy, cx+WWIDTH, cy+HEIGHT);
  77.  /* highlight new "gadget" position */
  78.  RectFill(rp, x, y, x+WWIDTH, y+HEIGHT);
  79.  cx = x;
  80.  cy = y; /* establish a new current position */ 
  81. }
  82.  
  83. void DrawGads() /* put text into the "gadgets" we created */
  84. {
  85.  long i, j, n, len;
  86.  maxw = 568 / HSPACE; /* max gadget subdivisions across */
  87.  maxh = 68  / VSPACE; /* max gadget subdivs high in real gadget */
  88.  cy = TOP; /* start at the top "gadget" */
  89.  n = 0; /* select phrases one at a time */
  90.  for(j=0; j<maxh; j++) {
  91.   cx = LEFT;
  92.   for(i=0; i<maxw; i++) {
  93.    SetDrMd(rp,JAM2);
  94.    SetAPen(rp,1);
  95.    Move(rp,cx+1, cy+6);
  96.    len = strlen(phrases[n]);
  97.    Text(rp,phrases[n], MIN(len,MAXCHARS));
  98.    DrawBox(cx, cy);  /* box around the "gadget" */
  99.    cx += HSPACE;
  100.    n += 1;
  101.   }
  102.   cy += VSPACE;
  103.  }
  104. }
  105.  
  106. void main(argc, argv)
  107. long argc;
  108. char *argv[];
  109. {
  110.  int result;
  111.  GfxBase = (struct Library *)OpenLibrary("graphics.library", 0);
  112.  if (GfxBase == NULL)
  113.   goto cleanup;
  114.  IntuitionBase = (struct Library *)OpenLibrary("intuition.library", 0);
  115.  if(IntuitionBase == NULL)
  116.   goto cleanup;
  117.  w = OpenWindow(&nw); /* open a window */
  118.  rp = w->RPort;
  119.  DrawGads(); /* draw a defined number of "gadgets" */
  120.  cx = LEFT;
  121.  cy = TOP;
  122.  SetDrMd(rp, COMPLEMENT);
  123.  RectFill(rp, cx, cy, cx+WWIDTH, cy+HEIGHT); /* hilite first */
  124.  while(1) {
  125.   WaitPort(w->UserPort);
  126.   mg = (struct IntuiMessage *)GetMsg(w->UserPort);
  127.   while(mg) { /* empties the port before going to sleep again */
  128.    SAVEMSG
  129.    if((result = HandleEvent()) == 0)
  130.     break;
  131.    mg = (struct IntuiMessage *)GetMsg(w->UserPort);
  132.   } 
  133.   if(result == 0)
  134.    break;
  135.  }
  136. cleanup:
  137.  if(w)
  138.   CloseWindow(w);
  139.  if(GfxBase)
  140.   CloseLibrary(GfxBase);
  141.  if(IntuitionBase)
  142.   CloseLibrary(IntuitionBase);
  143. }
  144.  
  145. int DumpXY()
  146. {
  147.  long x_index, y_index, word_index;
  148.  long x, y, mm;
  149.  /* NOTE: calculations based on GZZ window! */
  150.  /* convert to coordinates relative to topleft of gadget */
  151.  x_index = (GX) /HSPACE;
  152.  y_index = (GY) /VSPACE;
  153.  if(x_index>maxw-1 || y_index>maxh-1 || x_index<0 || y_index<0)
  154.   return(-1);
  155.  /* within the box? */
  156.  if(GX - (x_index * HSPACE) > WWIDTH)
  157.   return(-1);
  158.  if(GY - (y_index * VSPACE) > HEIGHT)
  159.   return(-1);
  160.  x = LEFT+ (x_index * HSPACE);
  161.  y = TOP + (y_index * VSPACE);
  162.  word_index = (y_index * maxw) + x_index;
  163.  mm = 1 + word_index; /* which gadget number was that? */
  164.  if(strlen(phrases[mm-1]) == 0)
  165.   return(-1); /* inactive if no text */
  166.  ShowCursor(x,y); /* only one gadget selectable at a time */
  167.  return(mm); /* tell caller which gadget number was hit */
  168. }
  169.  
  170. int HandleEvent()
  171. {
  172.  int testcode, num;
  173.  SHORT gadgetid;
  174.  
  175.  testcode = 1;
  176.  switch(cl)
  177.  {
  178.   case CLOSEWINDOW:
  179.    return(FALSE);
  180.    break;
  181.   case GADGETDOWN:
  182.    gadgetid = ((struct Gadget *)ad)->GadgetID;
  183.    switch(gadgetid)
  184.    {
  185.     case 99: 
  186.      num = DumpXY(); /* num, if needed */
  187.      SetDrMd(rp,JAM2);
  188.      SetAPen(rp,1);
  189.      Move(rp,20,40);
  190.      if(num == -1) { 
  191.       Text(rp,"Outside range of active gadgets ",32);
  192.       break;
  193.      }
  194.      else {
  195.       Text(rp,"You just hit gadget number      ",32);
  196.       Move(rp,244,40);
  197.       if(num <= 9) {
  198.        Text(rp,itoa(num),1);
  199.       }
  200.       else if(num >=10 && num <= 19) {
  201.        Text(rp,itoa(1),1);
  202.        Text(rp,itoa(num-10),1);
  203.       }
  204.       else if(num >=20 && num <= 29) {
  205.        Text(rp,itoa(2),1);
  206.        Text(rp,itoa(num-20),1);
  207.       }
  208.       else if(num >=30 && num <= 39) {
  209.        Text(rp,itoa(3),1);
  210.        Text(rp,itoa(num-30),1);
  211.       }
  212.      }
  213.     default:
  214.      break;
  215.    } /* end of gadget switch */
  216.    cl = 0; /* null the event */
  217.   default:
  218.    break;
  219.  } /* end of switch cl */
  220.  return(testcode);
  221. }
  222.